package com.zj.leetcode.medium;

import java.util.*;

/**
 * @program algorithm
 * @description:
 * 49. 字母异位词分组
 * 烦死了 为什么想不到
 * @author: zhangbo
 * @create: 2022/01/09 12:40
 */
public class GroupAnagrams {
    public static void main(String[] args) {
        String[] strs = {"eat", "tea", "tan", "ate", "nat", "bat"};


        Solution solution = new GroupAnagrams().new Solution();
        System.out.println(solution.groupAnagrams(strs));
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public List<List<String>> groupAnagrams(String[] strs) {
            Map<String, List<String>> map = new HashMap<String, List<String>>();
            for (String str : strs) {
                char[] chars = str.toCharArray();
                Arrays.sort(chars);
                String key = new String(chars);
                List<String> list = map.getOrDefault(key, new ArrayList<>());
                list.add(str);
                map.put(key, list);
            }
            return new ArrayList<>(map.values());

        }

    }
//leetcode submit region end(Prohibit modification and deletion)

}
